﻿using MediatR;
using SheepCultivation.Common.Domain;
using SheepCultivation.Common.Domain.SheepFarmSetting;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.SheepFarmsetup
{
    public class SheepFarmSeachHandler : IRequestHandler<SheepFarmSeachCommon, Result>
    {
        public readonly IBaseRepository<Sheepfarm> SheepfarmRepository;
        public readonly IBaseRepository<Sheepfarmscale> SheepfarmscaleRepository;
        public readonly IBaseRepository<Department> DepartmentRepository;
        public readonly IBaseRepository<Province> ProvinceRepository;
        public readonly IBaseRepository<City> CityRepository;
        public readonly IBaseRepository<County> CountyRepository;

        public SheepFarmSeachHandler(IBaseRepository<Sheepfarm> sheepfarmRepository, IBaseRepository<Sheepfarmscale> sheepfarmscaleRepository, IBaseRepository<Department> departmentRepository, IBaseRepository<Province> provinceRepository, IBaseRepository<City> cityRepository, IBaseRepository<County> countyRepository)
        {
            SheepfarmRepository = sheepfarmRepository;
            SheepfarmscaleRepository = sheepfarmscaleRepository;
            DepartmentRepository = departmentRepository;
            ProvinceRepository = provinceRepository;
            CityRepository = cityRepository;
            CountyRepository = countyRepository;
        }

        public async Task<Result> Handle(SheepFarmSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var sa = new PageResult() { };
                var list = from a in SheepfarmRepository.GetAll()
                           join b in SheepfarmscaleRepository.GetAll()
                           on a.SheepfarmscaleId equals b.SheepfarmscaleId
                           join c in DepartmentRepository.GetAll()
                           on a.DepartmentId equals c.DepartmentId
                           join d in ProvinceRepository.GetAll()
                           on a.ProviceId equals d.ProvinceId
                           join e in CityRepository.GetAll() on a.CityId equals e.CityId
                           join f in CountyRepository.GetAll() on a.CountyId equals f.CountyId
                           select new SheepFarmDto
                           {
                               SheepfarmId = a.SheepfarmId,
                               SheepfarmName = a.SheepfarmName,
                               SheepfarmSimpleName = a.SheepfarmSimpleName,
                               SheepfarmscaleId = a.SheepfarmscaleId,
                               SheepfarmscaleName = b.SheepfarmscaleName,
                               ProviceId = a.ProviceId,
                               ProvinceName = d.ProvinceName,
                               CityId = a.CityId,
                               CityName = e.CityName,
                               CountyId = a.CountyId,
                               CountyName = f.CountyName,
                               Fulladdress = a.Fulladdress,
                               CreateBooksDate = a.CreateBooksDate,
                               SheepfarmHead = a.SheepfarmHead,
                               DepartmentId = a.DepartmentId,
                               DepartmentName = c.DepartmentName,
                               IsOpen = a.IsOpen,
                           };
                if (request.SheepfarmName != "")
                {
                    list = list.Where(x => x.SheepfarmName.Contains( request.SheepfarmName));
                }
                if (request.SheepfarmHead != "")
                {
                    list = list.Where(x => x.SheepfarmHead.Contains(request.SheepfarmHead));
                }
                sa.TotalCount = list.Count();
                sa.PageCount = (int)Math.Ceiling((list.Count()*1.0)/request.Pagesize);
                sa.data = list.OrderBy(x => x.SheepfarmId).Skip((request.Pageindex - 1) * request.Pagesize).Take(request.Pagesize).ToList();
                res.Data = sa;
                res.Code = (int)CommonEnumCode.success;
                res.Msg = "羊场设置表新增";
            }
            catch (Exception)
            {

                throw;
            }

            return res;
        }
    }
    public class PageResult
    {
        public int PageCount { get; set; }
        public int TotalCount { get; set; }
        public List<SheepFarmDto> data { get; set; }
    }
    public class SheepFarmDto
    {
        public int SheepfarmId { get; set; }
        /// <summary>
        /// 羊场全称
        /// </summary>
        public string SheepfarmName { get; set; }
        /// <summary>
        /// 羊场简称
        /// </summary>
        public string SheepfarmSimpleName { get; set; }
        /// <summary>
        /// 羊场规模id
        /// </summary>
        public int SheepfarmscaleId { get; set; }
        public string SheepfarmscaleName { get; set; }
        /// <summary>
        /// 省id
        /// </summary>
        public int ProviceId { get; set; }
        public string ProvinceName { get; set; }
        /// <summary>
        /// 市id
        /// </summary>
        public int CityId { get; set; }
        public string CityName { get; set; }
        /// <summary>
        /// 县id
        /// </summary>
        public int CountyId { get; set; }
        public string CountyName { get; set; }
        /// <summary>
        /// 详细地址
        /// </summary>
        public string Fulladdress { get; set; }
        /// <summary>
        /// 建账日期
        /// </summary>
        public DateTime CreateBooksDate { get; set; }
        /// <summary>
        /// 羊场负责人
        /// </summary>
        public string SheepfarmHead { get; set; }
        /// <summary>
        /// 管理部门
        /// </summary>
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public int IsOpen { get; set; }
    }
}
